package cn.bdqn.pn.service.base.impl;

import cn.bdqn.pn.params.comment.CommentParams;
import cn.bdqn.pn.pojo.base.Comment;
import cn.bdqn.pn.pojo.base.Img;
import cn.bdqn.pn.reposity.base.CommentRepository;
import cn.bdqn.pn.reposity.base.ImageRepository;
import cn.bdqn.pn.service.base.CommentService;
import cn.bdqn.pn.utils.UpdateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private ImageRepository imageRepository;

    //商品名，商品时间，有图无图，分页，查询评论
    @Override
    public Page<Comment> getComments(CommentParams params,Integer page,Integer size) {
        Specification<Comment> specification=new Specification<Comment>() {
            @Override
            public Predicate toPredicate(Root<Comment> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                if(params.getCommodityId()!=null&&params.getCommodityId()!=0){    //商品id
                    list.add(cb.equal(root.get("commodityid"),params.getCommodityId()));
                }
                if(params.getImgId()!=null&&!params.getImgId().equals("")){
                    list.add(cb.equal(root.get("imgid"),params.getImgId()));
                }
                if(params.getBelongtoid()!=null){
                    list.add(cb.equal(root.get("belongtoid"),params.getBelongtoid()));
                }
                if(params.getStartDate() != null&&!params.getStartDate().equals("")){
                    list.add(cb.greaterThan(root.get("commenttime"),params.getStartDate()));
                }
                if(params.getEndDate() != null&&!params.getEndDate().equals("")){
                    list.add(cb.lessThan(root.get("commenttime"),params.getEndDate()));
                }
                Predicate[] predicates=new Predicate[list.size()];
                return cb.and(list.toArray(predicates));
            }
        };
        Pageable pageable = new PageRequest(page=page==null?0:page,size=size==null?5:size);
        /*Pageable pageable = new PageRequest(page=page==null?0:page,size=size==null?5:size,new Sort(new Sort.Order(Sort.Direction.DESC,"commenttime")));*/
        Page<Comment> commentList=commentRepository.findAll(specification,pageable);
        return commentList;
    }



    //修改状态（修改状态1前后台可见2前台不可见）
    @Override
    public Boolean updateByStatus(Comment comment) {
        Boolean flag=true;
        try {
            Comment source=commentRepository.getOne(comment.getCommentid());
            UpdateTool.copyNullProperties(source,comment);
            commentRepository.save(comment);
        }catch (RuntimeException e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }


    /**
     * 获取图片
     * @param id
     * @return
     */
    @Override
    public Img getCommentImages(Integer id) {
        return imageRepository.findAllByReleidAndImgtypeid(id,4);
    }

    /**
     * 查询商品评价星级占比率
     * @return  等级-数量
     */
    @Override
    public Map<String, Integer> getPriceSumByDay(Integer id) {

        List<Comment> list=commentRepository.getCommentByCommodityid(id.longValue());   //获取查询集合
        Map<String,Integer> LeveCount=new HashMap<>();  //定义一个map集合

        for(int i=0;i<list.size();i++){
            Comment comment=list.get(i);    //for循环获取对象
            if(LeveCount.get(comment.getLevel().toString())==null){     //通过key获取value值，如果值为null，创建一个值为0
                LeveCount.put(comment.getLevel().toString(),0);
            }
            LeveCount.put(comment.getLevel().toString(),LeveCount.get(comment.getLevel().toString())+1);
        }
        return LeveCount;
    }


}
